package com.example.run.teacherhelper.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.example.run.teacherhelper.R;
import com.example.run.teacherhelper.module.Student;

import java.util.List;

/**
 * Created by run on 2015/10/23.
 */
public class Student_info_Adapter extends BaseAdapter {
    private Context context;
    private List<Student> stuList;
    private int mScreemWidth;

    public Student_info_Adapter(Context context,List<Student> mlist,int mScreemWidth){
        this.context=context;
        this.stuList=mlist;
        this.mScreemWidth=mScreemWidth;
    }
    @Override
    public int getCount() {
        return stuList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Student student=stuList.get(position);
        final ViewHolder viewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.student_list_item,parent,false);
            viewHolder=new ViewHolder();
//             整个item的水平滚动层
            viewHolder.mHorizontalScrollView=(HorizontalScrollView)
                    convertView.findViewById(R.id.stu_sclv);
//           显示学生信息
            viewHolder.normalItemContentLayout=convertView.findViewById(R.id.stu_info);
//           操作按钮层
            viewHolder.operateLayout=convertView.findViewById(R.id.stu_operate);
            viewHolder.btn1=(Button)convertView.findViewById(R.id.fun_1);
            viewHolder.btn2=(Button)convertView.findViewById(R.id.fun_2);
            viewHolder.btn3=(Button)convertView.findViewById(R.id.fun_3);
            viewHolder.stuName=(TextView)convertView.findViewById(R.id.stu_name);
            viewHolder.stuNum=(TextView)convertView.findViewById(R.id.stu_number);

            //把位置放到view中,这样点击事件就可以知道点击的是哪一条item
            viewHolder.btn1.setTag(position);
            viewHolder.btn2.setTag(position);
            viewHolder.btn3.setTag(position);
            // 显示学生信息
            viewHolder.stuNum.setText(student.getId());
            viewHolder.stuName.setText(student.getName());

            //设置内容view的大小为屏幕宽度,这样按钮就正好被挤出屏幕外
            viewHolder.normalItemContentLayout = convertView.findViewById(R.id.stu_info);
            LayoutParams lp = viewHolder.normalItemContentLayout.getLayoutParams();
            lp.width = mScreemWidth;
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder=(ViewHolder)convertView.getTag();
        }




        convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_UP:
                //  获取viewHolder
                        ViewHolder viewHolder1=(ViewHolder)v.getTag();
                //  获取滑动初始位置
                        int scrollX=viewHolder.mHorizontalScrollView.getScrollX();
                //  获取操作区宽度
                        int operateW=viewHolder.operateLayout.getWidth();
                        //注意使用smoothScrollTo,这样效果看起来比较圆滑,不生硬
                        //如果水平方向的移动值<操作区域的长度的一半,就复原
                        if (scrollX <operateW/2)
                        {
                            viewHolder.mHorizontalScrollView.smoothScrollTo(0, 0);
                        }
                        else//否则的话显示操作区域
                        {
                            viewHolder.mHorizontalScrollView.smoothScrollTo(operateW, 0);
                        }
                        return true;
                }
                return false;
            }
        });
        return convertView;
    }

    public class ViewHolder{
        public HorizontalScrollView mHorizontalScrollView;// 整个item的水平滚动层
        public View normalItemContentLayout;//为未滑动时显示的布局
        public View operateLayout;//操作按钮层
        public Button btn1;
        public Button btn2;
        public Button btn3;
        public TextView stuName;
        public TextView stuNum;
    }
}
